package com.itheima.factory;

import com.itheima.service.UserService;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.io.InputStream;

public class BeanFactory {
    //根据指定对象id创建对应对象实例
    public static Object getBean(String id){
        //生命返回对象
        Object result=null;
        try {
            //1.通过类加载器加载beans.xml配置文件，获取io流
        InputStream in = BeanFactory.class.getClassLoader().getResourceAsStream("beans.xml");
        // 2.创建dom4j核心解析器对象
        SAXReader saxReader=new SAXReader();
        // 3.读取io流，解析xml，创建document对象
            Document document=saxReader.read(in);
            // 4.编写xpath表达式
            String xpath="//bean[@id='"+id+"']";
            // 5.查找对应bean标签 element对象
            Element element = (Element) document.selectSingleNode(xpath);
            // 6.获取该标签的，class属性值，全限定类名
            String className = element.attributeValue("class");
            // 7.通过反射机制，加载到jvm虚拟机
            Class<?> clazz = Class.forName(className);
            // 8.创建对象实例
            result = clazz.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void main(String[] args) {
        UserService userService = (UserService) BeanFactory.getBean("userService");
        System.out.println(userService.findByUsername("tom"));
    }
}
